package org.zero.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.zero.model.User;

/**
 * SessionAttributes：
 * <p>
 * 作用：暂存Model的数据到会话域
 * 属性：
 * names：同value
 * types：存入数据的类型
 *
 * @author Zero
 */
@Controller
@RequestMapping("/session_attribute")
@SessionAttributes(names = "user", types = User.class)
public class SessionAttributeController {

    @RequestMapping("/m1")
    public String method1(User user, Model model) {
        System.out.println("方法1：");
        System.out.println("user：" + user);
        System.out.println("model：" + model);

        model.addAttribute("user", user);

        return "forward:../session-attribute.jsp";
    }

    /**
     * SessionAttribute：
     * <p>
     * 作用：获取会话域数据
     * 属性：
     * name：同value
     * required：该值是否必须
     *
     * @author Zero
     */
    @RequestMapping("/m2")
    private String method2(@SessionAttribute(required = false) User user) {
        System.out.println("方法2：user：" + user);
        return "forward:../session-attribute.jsp";
    }
}
